################################################################################
##
## Purpose: This script creates Table 1, along with SI figure 11 and
##          SI tables 11.
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/tables/MS_table_1.tex
##  - ./output/tables/SI_table_11.tex
##  - ./output/figures/SI_figure_10.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(fixest)
require(sensemakr)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')

utterance_level <- utterance_level %>%
  arrange(docID,fullInd) %>%
  mutate_at(vars(matches('topic70.*_\\d')),list(lag = ~lag(.)))

# Rigorous fixed effects
dyadToAnal <- utterance_level %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'))

dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))


summary(modDyad1 <- feols(as.formula(paste0('interruptor ~ respondingTo')),
                         dyadToAnal %>% filter(all > 30,ind > mind),
                         cluster = 'opensecretsID + respondingTo'))

summary(modDyad2 <- feols(as.formula(paste0('interruptor ~ respondingTo + poly(scale(log(tot_utterances)),3)',
                                           ' + scale(age) + scale(votepct) + scale(nominate_dim1) + GOP + gender + scale(seniority) + scale(constrain_empower_tot) + yellen_vote + chamber')),
                         dyadToAnal %>% 
                           filter(all > 30,ind > mind),
                         cluster = 'opensecretsID + respondingTo'))

summary(modDyad3 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                           paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                           ' + ',
                                           paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                           ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                           ' + scale(age) + scale(votepct) + scale(nominate_dim1) + GOP + gender + scale(seniority) + scale(constrain_empower_tot) + yellen_vote + chamber',
                                           ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted')),
                         dyadToAnal %>% 
                           filter(all > 30,ind > mind),
                         cluster = 'opensecretsID + respondingTo'))

summary(modDyad4 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                           paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                           ' + ',
                                           paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                           ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                           ' + scale(age) + scale(votepct) + scale(nominate_dim1) + GOP + gender + scale(seniority) + scale(constrain_empower_tot) + yellen_vote + chamber',
                                           ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted',
                                           '| docID')),
                         dyadToAnal %>% 
                           filter(all > 30,ind > mind),
                         cluster = 'opensecretsID + respondingTo'))

summary(modDyad5 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                           paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                           ' + ',
                                           paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                           ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                           ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted',
                                           '| opensecretsID + docID')),
                         dyadToAnal %>% 
                           filter(all > 30,ind > mind),
                         cluster = 'opensecretsID + respondingTo'))

summary(modDyad6 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                           paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                           ' + ',
                                           paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                           ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                           ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted',
                                           '| opensecretsID + docID')),
                         dyadToAnal %>% 
                           filter(all > 30,ind > mind,
                                  yellen_vote != 1),
                         cluster = 'opensecretsID + respondingTo'))


dict <- c('respondingToFEDYELLEN' = 'Yellen (ref. Bernanke)','respondingToFEDPOWELL' = 'Powell (ref. Bernanke)',
          'respondingToFEDGREENSPAN' = 'Greenspan (ref. Bernanke)','scale(age)' = 'Age (scaled)',
          'scale(votepct)' = 'Vote Share (scaled)','scale(nominate_dim1)' = 'Ideology (scaled)',
  'GOP' = 'Republican (ref. Democrat)','chamberSenate' = 'Senate (ref. House)',
  'genderM' = 'Male (ref. Female)','scale(seniority)' = 'Seniority (scaled)','scale(constrain_empower_tot)' = 'Fed Oversight Sponsor',
  'yellen_vote' = 'Oppose Yellen Conf.','interrupted' = 'Interrupted','docID' = 'Hearing','opensecretsID' = 'Speaker')

etable(modDyad1,modDyad2,modDyad3,modDyad4,modDyad5,modDyad6,
       keep = 'ref. |scaled|Oversight|Oppose|Interrupted',
       order = c('^Yellen','Powell','Greenspan','Age','Vote','Ideology','Republican','Senate','Male','Seniority','Oversight','Oppose','Interr'),
       dict = dict,extralines = list('100 LDA Topics' = c('No','No','Yes','Yes','Yes','Yes'),
                                     'Tone Probabilities' = c('No','No','Yes','Yes','Yes','Yes')),
       depvar = F,digits = 3,digits.stats = 3,signif.code = c('***' = .001,'**' = .01,'*' = .05,'\\dag' = .1),replace = T,
       headers = list(c('','Controls:','Controls:','FEs:','FEs:','Dropping'),
                      c('Vanilla','Speaker','Utterance','Hearing','Speaker','Voters')),
       file = './output/tables/MS_table_1.tex')


# Robustness and sensitivity analysis
summary(modDyad1 <- feols(as.formula(paste0('interruptor ~ respondingTo')),
                          dyadToAnal %>% filter(all > 0,ind > mind),
                          cluster = 'opensecretsID + respondingTo'))

summary(modDyad2 <- feols(as.formula(paste0('interruptor ~ respondingTo + poly(scale(log(tot_utterances)),3)',
                                            ' + scale(age) + scale(votepct) + scale(nominate_dim1) + GOP + gender + scale(seniority) + scale(constrain_empower_tot) + yellen_vote + chamber')),
                          dyadToAnal %>% 
                            filter(all > 0,ind > mind),
                          cluster = 'opensecretsID + respondingTo'))

summary(modDyad3 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                            paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                            ' + ',
                                            paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                            ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                            ' + scale(age) + scale(votepct) + scale(nominate_dim1) + GOP + gender + scale(seniority) + scale(constrain_empower_tot) + yellen_vote + chamber',
                                            ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted')),
                          dyadToAnal %>% 
                            filter(all > 0,ind > mind),
                          cluster = 'opensecretsID + respondingTo'))

summary(modDyad4 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                            paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                            ' + ',
                                            paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                            ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                            ' + scale(age) + scale(votepct) + scale(nominate_dim1) + GOP + gender + scale(seniority) + scale(constrain_empower_tot) + yellen_vote + chamber',
                                            ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted',
                                            '| docID')),
                          dyadToAnal %>% 
                            filter(all > 0,ind > mind),
                          cluster = 'opensecretsID + respondingTo'))

summary(modDyad5 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                            paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                            ' + ',
                                            paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                            ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                            ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted',
                                            '| opensecretsID + docID')),
                          dyadToAnal %>% 
                            filter(all > 0,ind > mind),
                          cluster = 'opensecretsID + respondingTo'))

summary(modDyad6 <- feols(as.formula(paste0('interruptor ~ respondingTo + ',
                                            paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                            ' + ',
                                            paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                            ' + scale(SENT_combAttack_lag) + scale(SENT_combIncoh_lag) + scale(SENT_combToxic_lag)',
                                            ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted',
                                            '| opensecretsID + docID')),
                          dyadToAnal %>% 
                            filter(all > 0,ind > mind,
                                   yellen_vote != 1),
                          cluster = 'opensecretsID + respondingTo'))

dict <- c('respondingToFEDYELLEN' = 'Yellen (ref. Bernanke)','respondingToFEDPOWELL' = 'Powell (ref. Bernanke)',
          'respondingToFEDGREENSPAN' = 'Greenspan (ref. Bernanke)','scale(age)' = 'Age (scaled)',
          'scale(votepct)' = 'Vote Share (scaled)','scale(nominate_dim1)' = 'Ideology (scaled)',
          'GOP' = 'Republican (ref. Democrat)','chamberSenate' = 'Senate (ref. House)',
          'genderM' = 'Male (ref. Female)','scale(seniority)' = 'Seniority (scaled)','scale(constrain_empower_tot)' = 'Fed Oversight Sponsor',
          'yellen_vote' = 'Oppose Yellen Conf.','interrupted' = 'Interrupted','docID' = 'Hearing','opensecretsID' = 'Speaker')

etable(modDyad1,modDyad2,modDyad3,modDyad4,modDyad5,modDyad6,
       keep = 'ref. |scaled|Oversight|Oppose|Interrupted',
       order = c('^Yellen','Powell','Greenspan','Age','Vote','Ideology','Republican','Senate','Male','Seniority','Oversight','Oppose','Interr'),
       dict = dict,extralines = list('100 LDA Topics' = c('No','No','Yes','Yes','Yes','Yes'),
                                     'Tone Probabilities' = c('No','No','Yes','Yes','Yes','Yes')),
       depvar = F,digits = 3,digits.stats = 3,signif.code = c('***' = .001,'**' = .01,'*' = .05,'\\dag' = .1),replace = T,
       headers = list(c('','Controls:','Controls:','FEs:','FEs:','Dropping'),
                      c('Vanilla','Speaker','Utterance','Hearing','Speaker','Voters')),file = './output/tables/SI_table_11.tex')


# Sensitivity
summary(modDyad5 <- lm(as.formula(paste0('interruptor ~ yellen + ',
                                         paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                         ' + ',
                                         paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                         ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted',
                                         ' + factor(opensecretsID) + factor(docID)')),
                       dyadToAnal %>% 
                         mutate(yellen = ifelse(grepl('YELLEN',respondingTo),1,0)) %>% 
                         filter(all > 0,ind > mind,!grepl('FED',opensecretsID))))

sensResTmp <- try(sensemakr(model = modDyad5,
                            treatment = "yellen",
                            benchmark_covariates = 'interrupted',
                            kd = c(12,24),
                            ky = c(12,24),
                            q = 1,
                            alpha = 0.05,
                            reduce = TRUE))

pdf('./output/figures/SI_figure_10.pdf',width = 7,height = 7)
plot(sensResTmp,label.bump.y = 0,cex.lab = 1,cex.label.text = .6,round = 2)
abline(0,sensResTmp$bounds$r2yz.dx/sensResTmp$bounds$r2dz.x,col = 'grey60')
dev.off()

# EOF